package com.sxm.yiwei.adapters.pinche;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;

import com.squareup.picasso.Picasso;
import com.sxm.yiwei.R;
import com.sxm.yiwei.beans.pinche.PassengerOrder;
import com.sxm.yiwei.constant.Common;
import com.sxm.yiwei.tools.CircleTransform;
import com.sxm.yiwei.views.CircleImage;

import java.util.ArrayList;


/**
 * Created by Administrator on 2017/10/19.
 */

public class PassengerOrdersAdapter extends BaseAdapter{
    private final LayoutInflater inflater;
    private ArrayList<PassengerOrder> list;
    private Context context;
    private OnButtonClickListener mOnButtonClickListener;
    public PassengerOrdersAdapter(Context context,ArrayList<PassengerOrder> list) {
        this.list=list;
        this.context=context;
        inflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int i) {
        return null;
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    @Override
    public View getView(final int i, View view, ViewGroup viewGroup) {
        ViewHolder holder=null;
        if (view==null){
            view=inflater.inflate(R.layout.passenger_order_list_item,null);
            holder=new ViewHolder();
            holder.icon=(CircleImage) view.findViewById(R.id.iv_icon);
            holder.phone=(TextView)view.findViewById(R.id.tv_phone);
            holder.isOnline=(TextView)view.findViewById(R.id.tv_is_online);
            holder.words=(TextView)view.findViewById(R.id.tv_words);
            holder.time=(TextView)view.findViewById(R.id.tv_recent_come);
            holder.startPos=(TextView)view.findViewById(R.id.tv_start_pos);
            holder.endPos=(TextView)view.findViewById(R.id.tv_end_pos);
            holder.seats =(TextView)view.findViewById(R.id.tv_transfer_fee);
            holder.total_price=(TextView)view.findViewById(R.id.tv_total_price);
            holder.loss_money=(TextView)view.findViewById(R.id.tv_loss_money);
            holder.departTime=(TextView)view.findViewById(R.id.tv_depart_time);
            holder.weiBtn=(TextView)view.findViewById(R.id.btn_wei);
            holder.robBtn =(TextView)view.findViewById(R.id.btn_rob);
            view.setTag(holder);

        }else {
            holder = (ViewHolder) view.getTag();
        }
        PassengerOrder passengerOrder = list.get(i);
        Picasso.with(context).load(Common.IMAGE_HOST+passengerOrder.getIcon()).error(R.mipmap.morentouxiang).transform(new CircleTransform()).into(holder.icon);
        String phone = passengerOrder.getPhone();
        phone=phone.substring(0,3)+"****"+phone.substring(7,11);
        holder.phone.setText(phone);
        if (TextUtils.isEmpty(passengerOrder.getWords())){
            holder.words.setText("乘客留言：乘客很懒，没有留言");
        }else {
            holder.words.setText("乘客留言："+passengerOrder.getWords());
        }
        holder.words.setCompoundDrawablesWithIntrinsicBounds(context.getResources().getDrawable(R.mipmap.chengke),null,null,null);
        holder.time.setText(passengerOrder.getStart_time());
        holder.startPos.setText(passengerOrder.getStart_place());
        holder.endPos.setText(passengerOrder.getEnd_place());
        holder.total_price.setText(passengerOrder.getTotal_price()+"元");
        holder.seats.setText(passengerOrder.getSeats_need()+"个");
        holder.loss_money.setText(passengerOrder.getLoss_money()+"元");
        holder.departTime.setText(passengerOrder.getDepart_time());

        holder.weiBtn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    mOnButtonClickListener.onWeiClick(i);
                }
            });
        holder.robBtn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    mOnButtonClickListener.onRobClick(i);
                }
            });
        return view;
    }
    private class ViewHolder{
        CircleImage icon;
        TextView phone,isOnline,words,time,startPos,endPos, seats,total_price,loss_money,departTime;
        TextView weiBtn, robBtn;

    }
    public void setOnButtonClickListener(OnButtonClickListener mOnButtonClickListener){
        this.mOnButtonClickListener=mOnButtonClickListener;
    }
    public interface OnButtonClickListener{
        void onWeiClick(int position);
        void onRobClick(int position);
    }
}
